<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Forpastime <Forpastime@qq.com>
// +----------------------------------------------------------------------

namespace api\partner\model;

use think\db\Query;
use think\Model;
use think\Db;

class ShopApplyOrderModel extends Model
{
    public function getList($page,$partner_id,$status,$category_id,$localtion)
    {
        $childCateIds = [];
        if(!$category_id){
            $category_parent_id = Db::name('tenant_category')->where('id',$category_id)->value('parent_id');
            if($category_parent_id != 0){
                $childCate = Db::name('tenant_category')->where('id',$category_parent_id)->select();
            }else{
                $childCate = Db::name('tenant_category')->where('id',$category_id)->select();
            }
            foreach($childCate as $k=> $v){
                $childCateIds[$k]=$v['id'];
            }
            $list = $this->alias('a')
                ->leftJoin('for_shop_info i',['a.shop_info_id = i.id'])
                ->where(function (Query $query) use ($partner_id,$status){
                    if($partner_id>0){
                        $query->where('a.partner_id',$partner_id);
                    }
                    if($status){
                        $query->where('i.checked',$status);
                    }

                })
                ->where('i.category_id','in',$childCateIds)
                ->limit($page*10,10)
                ->select();
        }else{
            $list = $this->alias('a')
                ->leftJoin('for_shop_info i',['a.shop_info_id = i.id'])
                ->where(function (Query $query) use ($partner_id,$status){
                    if($partner_id>0){
                        $query->where('a.partner_id',$partner_id);
                    }
                    if($status){
                        $query->where('i.checked',$status);
                    }
                })
                ->where('i.checked',$status)
                ->limit($page*10,10)
                ->select();
        }
        

        return $list;
    }

    /**
     * 计算两点之间的距离
     * @param $lng1 经度1
     * @param $lat1 纬度1
     * @param $lng2 经度2
     * @param $lat2 纬度2
     * @param int $unit m，km
     * @param int $decimal 位数
     * @return float
     */
    function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2)
    {

        $EARTH_RADIUS = 6370.996; // 地球半径系数
        $PI           = 3.1415926535898;

        $radLat1 = $lat1 * $PI / 180.0;
        $radLat2 = $lat2 * $PI / 180.0;

        $radLng1 = $lng1 * $PI / 180.0;
        $radLng2 = $lng2 * $PI / 180.0;

        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;

        $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $distance = $distance * $EARTH_RADIUS * 1000;

        if ($unit === 2) {
            $distance /= 1000;
        }

        return round($distance, $decimal);
    }

    public function getOne($partner_id,$order_id)
    {
        $order = $this->where(['partner_id'=>$partner_id,'shop_info_id'=>$order_id])->find();
        if($order){
            $shop_info = Db::name('shop_info')->where('id',$order_id)->find();
            return $shop_info;
        }
            
    }
}